//
//  RealTimeNavigationView.m
//  sdk2.0zhengquandasha
//
//  Created by Choi on 15/12/21.
//  Copyright © 2015年 palmaplus. All rights reserved.
//

#import "RealTimeNavigationView.h"
@interface RealTimeNavigationView()
@property (nonatomic,assign)int distanceFromEndPoint;
@property (nonatomic,strong)UILabel *labelDistance;
@property (nonatomic,strong)UILabel *labelTime;
@property (nonatomic,strong)UIButton *btnEndNavigation;
@end
@implementation RealTimeNavigationView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 2;
        [self initAll];
    }
    return self;
}

-(void)initAll{
    CGSize size = self.frame.size;
    CGFloat left = 20;
    CGFloat top = 5;
    CGSize labelSize = CGSizeMake(150, 30);
    CGSize btnSize = CGSizeMake(88, 44);
    _labelDistance = [[UILabel alloc]init];
    _labelDistance.frame = CGRectMake(left, top, size.width/2 , (size.height - 3*top) / 2);
   // _labelDistance.backgroundColor = [UIColor grayColor];
    [self addSubview:_labelDistance];
    
    _labelTime = [[UILabel alloc]init];
    _labelTime.frame = CGRectMake(left, CGRectGetMaxY(_labelDistance.frame) + top,size.width/2 , (size.height - 3*top) / 2);
   // _labelTime.backgroundColor = [UIColor grayColor];
    [self addSubview:_labelTime];
    
    _btnEndNavigation = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnEndNavigation.frame = CGRectMake(size.width - 10 -btnSize.width , (size.height - btnSize.height)/2, btnSize.width, btnSize.height);
    [_btnEndNavigation setBackgroundImage:[UIImage imageNamed:@"btn_end@2x"] forState:UIControlStateNormal];
    [_btnEndNavigation addTarget:self action:@selector(endNavigationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnEndNavigation];
    
}

-(void)endNavigationAction:(UIButton *)btn{
    NSLog(@"结束导航");
    [self removeFromSuperview];
    if (self.endNavBlock) {
        self.endNavBlock();
    }
}

-(void)setDistanceFromEndPoint:(int)distance{
    _distanceFromEndPoint = distance;
    NSString *strDistance = [NSString stringWithFormat:@"距离目的地%d米",distance];
    int time  = (int)distance/1.5;
    NSString *strTime = [NSString stringWithFormat:@"需要步行%d秒",time];
    
    NSString *tempDistance = [NSString stringWithFormat:@"%d",distance];
    NSString *tempTime = [NSString stringWithFormat:@"%d",time];
    
    _labelDistance.attributedText = [self createAttributedString:strDistance andNSRange:NSMakeRange(5, (int)tempDistance.length)];
    _labelTime.attributedText = [self createAttributedString:strTime andNSRange:NSMakeRange(4,(int)tempTime.length)];
    
}

-(NSMutableAttributedString *)createAttributedString:(NSString *)str andNSRange:(NSRange)range{
    NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:str];
//    [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:range];
    [attstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    return attstr;
}

@end
